<?php

namespace app\shopapi\controller;

use app\admin\model\Feedback;
use app\admin\model\Shop;
use app\admin\model\UserMessage;
use app\common\controller\Api;
use app\common\controller\Shopapi;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Admin;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 会员接口
 */
class User extends Shopapi
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third','wechatlogin'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     */
    public function index()
    {
        //$data = ['userinfo' => $this->shopauth->getUserinfo()];
        $shop = Shop::get(['username'=>$this->shopauth->username]);
        $this->success('',$shop);
    }

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @ApiParams (name="account", type="string", required=true, description="账号")
     * @ApiParams (name="password", type="string", required=true, description="密码")
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->shopauth->login($account, $password);
        if ($ret) {
            //$shop = Shop::get(['username'=>$this->shopauth->username]);
            $data = ['userinfo' => $this->shopauth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->shopauth->getError());
        }
    }

    /**
     * 手机验证码登录
     *
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="captcha", type="string", required=true, description="验证码")
     */
    public function mobilelogin()
    {
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        //$user = \app\common\model\User::getByMobile($mobile);
        $user = Admin::get(['mobile'=>$mobile]);
        if ($user) {
            if ($user->status != 'normal') {
                $this->error(__('Account is locked'));
            }
            //如果已经有账号则直接登录
            $ret = $this->shopauth->direct($user->id);
        } else {
            $this->error('账号不存在');
            //$ret = $this->shopauth->register($mobile, Random::alnum(), '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->shopauth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->shopauth->getError());
        }
    }


    /**
     * 重置密码
     *
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="newpassword", type="string", required=true, description="新密码")
     * @ApiParams (name="captcha", type="string", required=true, description="验证码")
     */
    public function resetpwd()
    {
        $mobile = $this->request->post("mobile");
        $newpassword = $this->request->post("newpassword");
        $captcha = $this->request->post("captcha");
        if (!$newpassword || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        //验证Token
        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
            $this->error(__('Password must be 6 to 30 characters'));
        }

        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        //$user = \app\common\model\User::getByMobile($mobile);
        $user = Admin::get(['mobile'=>$mobile]);
        if (!$user) {
            $this->error(__('User not found'));
        }
        $ret = Sms::check($mobile, $captcha, 'resetpwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'resetpwd');

        //模拟一次登录
        $this->shopauth->direct($user->id);
        $ret = $this->shopauth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->shopauth->getError());
        }
    }

    //修改密码
    public function changepwd()
    {
        $mobile = $this->request->post("mobile");
        $newpassword = $this->request->post("newpassword");
        $oldpassword = $this->request->post("oldpassword");
        $captcha = $this->request->post("captcha");
        if (!$newpassword || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        //验证Token
        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
            $this->error(__('Password must be 6 to 30 characters'));
        }

        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        //$user = \app\common\model\User::getByMobile($mobile);
        $user = Admin::get(['mobile'=>$mobile]);
        if (!$user) {
            $this->error(__('User not found'));
        }
        $ret = Sms::check($mobile, $captcha, 'changepwd');
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        Sms::flush($mobile, 'changepwd');

        //模拟一次登录
        $this->shopauth->direct($user->id);
        $ret = $this->shopauth->changepwd($newpassword, $oldpassword, true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->shopauth->getError());
        }
    }

    /**
     * Notes:微信登录
     * User: gai
     * Date: 2022/12/26
     * Time: 10:40
     */
    public function wechatlogin(){
        $params = input();

        $ret = $this->shopauth->wechat_login($params);
        if ($ret) {
            $info = $this->shopauth->getUserinfo();
            $data = ['userinfo' =>$info ];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->shopauth->getError());
        }
    }


}
